import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;


public class MultipleChoiceResultView extends ResultView implements IView
{
	private JDialog resultDialog;
	private MultipleChoiceQuestion itsQuestion;
	
	public MultipleChoiceResultView(MultipleChoiceQuestion question, String userAnswer)
	{
		super(userAnswer);
		itsQuestion = question;
	}

	@Override
	public void display(Window parent)
	{
		boolean wrong = !userAnswer.equals(itsQuestion.getAnswer());
		
		// dialog
		resultDialog = new JDialog(parent, itsQuestion.getTitle(), Dialog.ModalityType.DOCUMENT_MODAL);
		// question label
		String s = new String(wrong ? "- Wrong!" : " - Correct!");
		JLabel textLabel = new JLabel(itsQuestion.getText() + s);
		textLabel.setForeground(wrong ? Color.red : new Color(0, 100, 0));
		textLabel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 0));
		// image
		JLabel picture = new JLabel(createImageIcon(itsQuestion.getImageFile()));
		// radio buttons
		JRadioButton s1Btn = new JRadioButton(itsQuestion.getSelection(0)); if(userAnswer.equals("0")) s1Btn.setSelected(true);
		JRadioButton s2Btn = new JRadioButton(itsQuestion.getSelection(1)); if(userAnswer.equals("1")) s2Btn.setSelected(true);
		JRadioButton s3Btn = new JRadioButton(itsQuestion.getSelection(2)); if(userAnswer.equals("2")) s3Btn.setSelected(true);
		JRadioButton s4Btn = new JRadioButton(itsQuestion.getSelection(3)); if(userAnswer.equals("3")) s4Btn.setSelected(true);
		s1Btn.setEnabled(false);
		s2Btn.setEnabled(false);
		s3Btn.setEnabled(false);
		s4Btn.setEnabled(false);
		
		ButtonGroup group = new ButtonGroup();
		group.add(s1Btn);
		group.add(s2Btn);
		group.add(s3Btn);
		group.add(s4Btn);
		
		// panel for radio buttons and icon labels
		JPanel radioPanel = new JPanel(new GridLayout(0, 1));
		GroupLayout grp = new GroupLayout(radioPanel);
		radioPanel.setLayout(grp);
		grp.setAutoCreateGaps(true);
		grp.setAutoCreateContainerGaps(true);
		GroupLayout.SequentialGroup hGroup = grp.createSequentialGroup();
		
		// make the layout nice :)
		JLabel[] msgLabels = getMessageLabels();
		JLabel[] pictureLabels = getPictureLabels();
		hGroup.addGroup(grp.createParallelGroup().addComponent(s1Btn).addComponent(s2Btn).addComponent(s3Btn).addComponent(s4Btn));
		hGroup.addGroup(grp.createParallelGroup().addComponent(msgLabels[0]).addComponent(msgLabels[1]).addComponent(msgLabels[2]).addComponent(msgLabels[3]));
		hGroup.addGroup(grp.createParallelGroup().addComponent(pictureLabels[0]).addComponent(pictureLabels[1]).addComponent(pictureLabels[2]).addComponent(pictureLabels[3]));
		
		grp.setHorizontalGroup(hGroup);
		GroupLayout.SequentialGroup vGroup = grp.createSequentialGroup();
		
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s1Btn).addComponent(msgLabels[0]).addComponent(pictureLabels[0]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s2Btn).addComponent(msgLabels[1]).addComponent(pictureLabels[1]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s3Btn).addComponent(msgLabels[2]).addComponent(pictureLabels[2]));
		vGroup.addGroup(grp.createParallelGroup(Alignment.BASELINE).addComponent(s4Btn).addComponent(msgLabels[3]).addComponent(pictureLabels[3]));
		grp.setVerticalGroup(vGroup);
		
        radioPanel.add(s1Btn);
        radioPanel.add(s2Btn);
        radioPanel.add(s3Btn);
        radioPanel.add(s4Btn);
		
        JPanel centerPanel = new JPanel();
        centerPanel.add(radioPanel, BorderLayout.LINE_START);
        centerPanel.add(picture, BorderLayout.CENTER);
        centerPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
		
		JButton doneBtn = new JButton("Done");
		doneBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				resultDialog.setVisible(false);
			}
		});
		
		resultDialog.getContentPane().add(textLabel, BorderLayout.NORTH);
		resultDialog.getContentPane().add(centerPanel, BorderLayout.CENTER);
		
		JPanel doneBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		doneBtnPanel.add(doneBtn);
		
		resultDialog.getContentPane().add(doneBtnPanel, BorderLayout.SOUTH);
		
		resultDialog.pack();
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - resultDialog.getWidth()) / 2;
		final int y = (screenSize.height - resultDialog.getHeight()) / 2;
		resultDialog.setLocation(x, y);
		
		resultDialog.setVisible(true);
	}
	
	private JLabel[] getPictureLabels()
	{
		JLabel[] labels = new JLabel[4];
		for(int i = 0; i < 4; i++) labels[i] = new JLabel("");
		if(userAnswer.equals(itsQuestion.getAnswer()))
		{
			// correct
			labels[Integer.parseInt(itsQuestion.getAnswer())].setIcon(createImageIcon("left-green-32.jpg"));
		}
		else
		{
			labels[Integer.parseInt(itsQuestion.getAnswer())].setIcon(createImageIcon("left-green-32.jpg"));
			labels[Integer.parseInt(userAnswer)].setIcon(createImageIcon("left-red-32.jpg"));
		}
		return labels;
	}

	private JLabel[] getMessageLabels()
	{
		JLabel[] labels = new JLabel[4];
		for(int i = 0; i < 4; i++) labels[i] = new JLabel("");
		labels[Integer.parseInt(userAnswer)].setText("your answer");
		if(!userAnswer.equals(itsQuestion.getAnswer()))
		{
			labels[Integer.parseInt(itsQuestion.getAnswer())].setText("correct answer");
		}
		return labels;
	}

	private ImageIcon createImageIcon(String filename)
	{
		return new ImageIcon(filename);
	}

}
